﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JTE_WCF_Client
{
    public static class Logger
    {
        private static string writeLock = "";
        /// <summary>
        /// 记录本地日志文件
        /// </summary>
        /// <param name="content">内容</param>
        public static void Error(this string content)
        {
            lock (writeLock)
            {
                var time = DateTime.Now;
                var dirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Logs", time.ToString("yyyyMM"));
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                //半小时一个日志文件
                string filePath = Path.Combine(dirPath, time.ToString("yyyyMMddHH") + ".log");
                using (StreamWriter sw = new StreamWriter(filePath, true, Encoding.UTF8))
                {
                    sw.WriteLine(time.ToString("yyyy-MM-dd HH:mm:ss") + $"[PMS Error]:{content}");
                }
            }
        }
        public static void Error(this Exception exception)
        {
            lock (writeLock)
            {
                var time = DateTime.Now;
                var dirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Logs", time.ToString("yyyyMM"));
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                //半小时一个日志文件
                string filePath = Path.Combine(dirPath, time.ToString("yyyyMMddHH") + ".log");
                using (StreamWriter sw = new StreamWriter(filePath, true, Encoding.UTF8))
                {
                    sw.WriteLine(time.ToString("yyyy-MM-dd HH:mm:ss") + $"[PMS Error]:{exception}");
                }
            }
        }
        /// <summary>
        /// 记录本地日志文件
        /// </summary>
        /// <param name="content">内容</param>
        public static void Info(this string content)
        {
            lock (writeLock)
            {
                var time = DateTime.Now;
                var dirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Logs", time.ToString("yyyyMM"));
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                //半小时一个日志文件
                string filePath = Path.Combine(dirPath, time.ToString("yyyyMMddHH") + ".log");
                using (StreamWriter sw = new StreamWriter(filePath, true, Encoding.UTF8))
                {
                    Console.WriteLine(time.ToString("yyyy-MM-dd HH:mm:ss") + $"[外挂 Info]:{content}");
                    sw.WriteLine(time.ToString("yyyy-MM-dd HH:mm:ss") + $"[PMS Info]:{content}");
                }
            }
        }
    }
}
